<template>
  <div class="form">
    <h2>登录</h2>
    <el-form
      style="margin-top: 40px"
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
    >
      <el-form-item prop="username">
        <el-input
          v-model="ruleForm.username"
          size="large"
          :prefix-icon="User"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="ruleForm.password"
          size="large"
          type="password"
          show-password
          :prefix-icon="Lock"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          size="large"
          style="width: 100%"
          @click="submitForm(ruleFormRef)"
        >
          登录
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-row justify="space-between" style="width: 100%" :gutter="5">
          <el-col :span="8"
            ><el-button size="large" style="width: 100%" @click="goto('phone')">
              手机登录
            </el-button></el-col
          >
          <el-col :span="8"
            ><el-button size="large" style="width: 100%" @click="goto('email')">
              邮箱登录
            </el-button></el-col
          >
          <el-col :span="8"
            ><el-button
              size="large"
              style="width: 100%"
              @click="goto('register')"
            >
              注册
            </el-button></el-col
          >
        </el-row>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { userLogin } from "../../api/userLogin/index";
import { ElMessage, ElMessageBox } from "element-plus";
import router from "../../router";
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  username: "",
  password: "",
});
const rules = reactive<FormRules>({
  username: [
    {
      required: true,
      message: "请输入账号",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "请输入密码",
      trigger: "blur",
    },
    {
      min: 6,
      max: 20,
      message: "密码在6-20为之间",
      trigger: "blur",
    },
  ],
});

//登录
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const res = await userLogin(ruleForm);
      if (res.data.success) {
        ElMessage({
          message: res.data.msg,
          type: "success",
        });
        sessionStorage.setItem("id", res.data.data[0].id);
        sessionStorage.setItem("userInfo", JSON.stringify(res.data.data[0]));

        router.push({ path: "/" });
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      console.log("error submit!", fields);
    }
  });
};

const goto = (type: string) => {
  if (type === "phone") {
    router.push("/login/phoneLogin");
  } else if (type === "email") {
    router.push("/login/emailLogin");
  } else if (type === "register") {
    router.push("/login/register");
  }
};
</script>

<style lang="scss" scoped>
.form {
  width: 400px;
  height: 500px;
  text-align: center;
  padding: 20px;
}
</style>
